'''
Created on Mar 14, 2016

@author: JimmyMo
'''
from writter.perfsynwritter import Writter
import redis
import time
from database import dao

class Writter4Redis(Writter):
    def __init__(self):
        super().__init__()
        self.rc_client = dao.get_redis_connection(self.host, self.port, self.p_db)
#         self.rc_client = redis.Redis(self.host, self.port, self.p_db)
        
    def _parse_options(self):
        self.host = "localhost" 
        self.port = 6379
#         self.c_db = 0
        self.p_db = 1
        self.buffer_size = 200000
    def _write(self):
        events = self.event_buffer_list
        pipe = self.rc_client.pipeline()
        pipe.multi()
        for e in events:
            if not 'portname' in e:
                portname = "_default"
            else:
                portname = e["portname"]
            key = "GRP:%s:DEV:%s:PRT:%s" % (e["_grp"], e["devicename"], portname)
#             print(key)
            pipe.set(key, e)
            
        now = time.clock()
        print("inserting to Redis...")    
        pipe.execute()
        print("done inserting")
        print("total elapsed %f..." % (time.clock() - now))
        pass